package info.cs52.fes.lp;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NatureLanguageParser extends LanguageParser{

	public final char[] MARKED_CHARS = {
			'á','à','ạ','ã','ả',
			'ă','ắ','ằ','ặ','ẵ','ẳ',
			'â','ấ','ầ','ậ','ẫ','ẩ',
			'đ',
			'é','è','ẹ','ẽ','ẻ',
			'ê','ế','ề','ệ','ễ','ể',
			'í','ì','ị','ĩ','ỉ',
			'ó','ò','ọ','õ','ỏ',
			'ô','ố','ồ','ộ','ỗ','ổ',
			'ơ','ớ','ờ','ợ','ỡ','ở',
			'ú','ù','ụ','ũ','ủ',
			'ư','ứ','ừ','ự','ữ','ử',
			'ý','ỳ','ỵ','ỹ','ỷ'
	};
	
	public final char[] UNMARKED_CHARS = {
			'a','a','a','a','a',
			'a','a','a','a','a','a',
			'a','a','a','a','a','a',
			'd',
			'e','e','e','e','e',
			'e','e','e','e','e','e',
			'i','i','i','i','i',
			'o','o','o','o','o',
			'o','o','o','o','o','o',
			'o','o','o','o','o','o',
			'u','u','u','u','u',
			'u','u','u','u','u','u',
			'ý','ỳ','ỵ','ỹ','ỷ'
	};
	
	
	@Override
	public void parse(){

		errorCode = 0;
		phraseType = 0;
		if ((originalPhrase==null) || (originalPhrase.isEmpty())){
			errorCode = ERR_NTT;
			translatedPhrase = "[rỗng]";
			return;
		} else {
			int i=0;
			String unmarkedOriginalPhrase = originalPhrase;
			for (char ch:MARKED_CHARS){
				unmarkedOriginalPhrase = unmarkedOriginalPhrase.replace(ch, UNMARKED_CHARS[i]);
				i++;
			}

			if (defaultPattern != null){
				for (PhrasePattern pattern : defaultPattern) {
					Pattern p = Pattern.compile(pattern.getPattern());
					Matcher m = p.matcher(unmarkedOriginalPhrase);
					if (m.matches()){
						phraseType = pattern.getType();
						translatedPhrase = pattern.getSimplePhrases()[0];
						break;
					}
				}
			}
		}
		if (phraseType == 0) {
			errorCode = ERR_DUP;
			translatedPhrase = "er_hc";
		}
	}
	


}
